<template>
	<Home ref="home" @tabChange="tabChange"></Home>
</template>

<script>
// import actLog from '@/components/modelParser/libs/actLog.js';
import Home from '@/components/modelParser/Home';
import getCurrentUri from '@/components/modelParser/libs/getCurrentUri.js';
import getSharePath from '@/components/modelParser/libs/getSharePath.js';
import variableChange from '@/components/modelParser/libs/variableChange.js';
import getAppInfo from '@/components/modelParser/libs/getAppInfo.js';
import getPageJson from '@/components/modelParser/libs/getPageJson.js';
import ifTerminalTrue from '@/components/modelParser/libs/ifTerminalTrue.js';
import projectInfo from '@/components/modelParser/projectInfo.js';
import myPromise from '@/components/modelParser/libs/myPromise.js';
// #ifdef H5
// 用于H5端预览页面----start----
// import getAppInfo_prev from '@/components/modelParser/libs/getAppInfo_prev.js';
// import getSearchParam from '@/components/modelParser/libs/getSearchParam.js';
// 用于H5端预览页面----end----
// #endif
export default {
	components: {
		Home
	},
	data() {
		return {
			pageData: {
				conData: [],
				appMenus: []
			},
			tobeUsedMenus: [],
			options: {},
			canDoOnshow: false
		};
	},
	provide(){
		return {
			options: ()=>this.options
		}
	},
	onShow() {
		if(this.canDoOnshow){
			this.$refs.home.pageState('show');
		}
	},
	onLoad(options) {
		uni.$on('homePageRefresh',()=>{
			this.$refs.home.pageState('created')
		})
		this.options = options;
		// #ifdef H5
		// 用于H5端预览页面----start----
		// var search = getSearchParam()
		// if (search.timestamp && new Date().getTime() - search.timestamp * 1 > 1000 * 60 * 10) {
		// 	uni.showToast({
		// 		title: '链接已失效',
		// 		icon: 'none'
		// 	});
		// 	return;
		// }
		// this.$u.vuex('$tag', search.tag);
		// this.$u.vuex('$version', search.version);
		// this.$u.vuex('$prevMode', true);
		// getAppInfo_prev().then(()=>{
		// 	this.getData();
		// })
		// return
		// 用于H5端预览页面----end----
		// #endif
		getAppInfo().then(appMenus=>{
			console.log('getAppInfo',appMenus);
			this.getData(appMenus);
		},()=>{
			setTimeout(()=>{
				uni.showToast({
					title: '加载项目资源失败',
					icon: 'none'
				});
			})
		}).finally(() => {
			uni.hideLoading();
		})
	},
	onUnload(){
		uni.$off('homePageRefresh')
	},
	methods: {
		getData(appMenus) {
			if(appMenus){
				this.getMenus(appMenus)
			}else{
				getPageJson(this.$appInfo.startPageId,'1').then(menus=>{
					this.getMenus(menus)
				})
			}
		},
		getMenus(menus){
			// 过滤筛选出相应运行端的菜单
			var appMenus = [];
			// 对菜单进行筛选
			menus.common.links = menus.common.links.filter(menu => ifTerminalTrue(menu.terminal));
			appMenus.push(menus);
			this.pageData.appMenus = appMenus;
			// 初始化tabbar
			this.$refs.home.pageData.appMenus = this.pageData.appMenus;
			if(this.pageData.appMenus.length > 0){
				this.$refs.home.tabbarInit()
			}
			// 请求剩余的json文件
			this.tobeUsedMenus = [];
			if (appMenus.length > 0) {
				var links = appMenus[0].common.links,
					selectedIndex = appMenus[0].common.originSelectIndex,
					selectedPageId = links[selectedIndex].pageId;
				var promises = [];
				var promise = this.getOnePageData(selectedPageId, undefined, true);
				promises.push(promise)
				var tobeUseArr = links.slice(0, selectedIndex).concat(links.slice(selectedIndex + 1));
				// 判断app菜单是否重复关联页面,只请求必要的json
				tobeUseArr.forEach((link, index) => {
					var ifRepeated = link.pageId === selectedPageId;
					if(!ifRepeated){
						var lastArr = tobeUseArr.slice(0,index);
						ifRepeated = lastArr.some(menu=>menu.pageId===link.pageId);
					}
					if (!ifRepeated) {
						var promise = this.getOnePageData(link.pageId, 'tobeUsedMenus', true);
						promises.push(promise)
					}
				});
				uni.showLoading({
					title: '加载中'
				});
				Promise.all(myPromise(promises)).then(res=>{
					if(res.length === promises.length){
						uni.hideLoading()
					}
				})
			} else {
				// 如果菜单为空,则所有页面均为空
				this.pageData.conData.splice(0);
			}
		},
		getOnePageData(pageId, menuBox, noLoading) {
			return new Promise((resolve,reject)=>{
				if (!pageId) {
					uni.stopPullDownRefresh()
					resolve()
					return;
				}
				getPageJson(pageId,'2', noLoading).then(page=>{
					uni.stopPullDownRefresh()
					if (!menuBox) {
						var ifRepeated = this.pageData.conData.some((item, index) => {
							if (item.cid === pageId) {
								// 用于下拉刷新页面时
								this.initHomePage(index,page)
								return true;
							}
						});
						if (!ifRepeated) {
							// 用于第一次加载页面时
							this.initHomePage(0,page)
						}
					} else {
						// 用于下载页面数据备用
						this[menuBox].push(page);
					}
					resolve()
				},()=>{
					resolve()
				})
			})
		},
		initHomePage(index,page){
			// 更新页面，并计算页面样式
			this.pageData.conData.splice(index,1,page);
			// #ifdef MP-TOUTIAO
			this.$nextTick(()=>{
			// #endif
			this.$refs.home.pageData.conData = this.pageData.conData;
			this.$refs.home.pageInit(index)
			this.canDoOnshow = true
			// #ifdef MP-TOUTIAO
			})
			// #endif
		},
		pageRefresh() {
			if (this.pageData.appMenus.length === 0) {
				this.getData();
			} else {
				var originSelectIndex = this.pageData.appMenus[0].common.originSelectIndex;
				var pageId = this.pageData.appMenus[0].common.links[originSelectIndex].pageId;
				console.log('originSelectIndex=', originSelectIndex, 'pageId=', pageId);
				this.getOnePageData(pageId);
			}
		},
		tabChange(pageId, broadP) {
			this.tobeUsedMenus.some((item, index) => {
				if (item.cid === pageId) {
					if (broadP) {
						//默认将广播传过来的数据保存到页面变量中
						for (var k in broadP) {
							variableChange({
								page: item,
								variableName: k,
								data: broadP[k]
							});
						}
					}
					// 点击切换时
					this.initHomePage(this.pageData.conData.length,item)
					this.tobeUsedMenus.splice(index, 1);
					return true;
				}
			});
		}
	},
	onPullDownRefresh() {
		this.pageRefresh()
	},
	onShareAppMessage(res) {
		var path, imageUrl = '';
		if (res.from === 'button') {
			var shareInfo = res.target.dataset.share;
			path = getSharePath(shareInfo, 'indexPage', this.pageData, this.options);
			console.log('path',path);
			imageUrl = shareInfo.image || '';
		} else {
			path = getCurrentUri();
		}
		// var nowPageId = '',source = 'indexPage'
		// if(source === 'indexPage'){
		// 	var originSelectIndex = this.pageData.appMenus[0].common.originSelectIndex;
		// 	nowPageId = this.pageData.appMenus[0].common.links[originSelectIndex].pageId;
		// } else if (source === 'singlePage'){
		// 	nowPageId = this.pageData.conData[0].cid
		// }
		// actLog({
		// 	shareLockCycle: 24,
		// 	pageCode: nowPageId,
		// 	itemType: 'share',
		// 	itemCode: '2',
		// 	itemName: '通用分享',
		// 	actCode: '',
		// 	themeCode: '',
		// 	withLoc: true,
		// },{},'',path)
		console.log('分享的路径',path);
		return {
			title: projectInfo.proName,
			path: path,
			imageUrl
		};
	},
	onShareTimeline() {
		return {
			title: projectInfo.proName
		};
	}
};
</script>